package shuashuami.hb.com.fragment.bussiness;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.YongjinAdapter;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.model.Yongjin;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class BYongJinMingxiFragment extends Fragment {

    private String type;
    private ListView listView;
    private List<Yongjin> list;

    public void setType(String type) {
        this.type = type;
    }

    private YongjinAdapter yongjinAdapter;

    private int page = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cyong_jin_mingxi, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        listView = (ListView) view.findViewById(R.id.lv_listview);
    }

    private void initData() {
        list = new ArrayList<>();
        getMessage();
    }

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(getActivity());
        HttpBMethods.getYongJinmingxi(listener, shareCUserInfo.getId(), page + "", type
                , "10"
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(getActivity());
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            JSONArray jsonArray = temp.getJSONArray("list");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject temp2 = jsonArray.getJSONObject(i);
                                Yongjin yongjin = new Yongjin( temp2.getString("money"),temp2.getString("addtime"));
                                list.add(yongjin);
                            }
                        } else {
                            ToastUtil.showShort(getActivity(), jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(getActivity(), "数据解析错误");
                    }
                    initAdapter();

                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void initAdapter() {
        yongjinAdapter = new YongjinAdapter(list, getActivity());
        listView.setAdapter(yongjinAdapter);
    }

}
